//
//  ViewController.m
//  Test
//
//  Created by 吕飞俊 on 15/11/24.
//  Copyright © 2015年 Goudu KuaiEx Technology(Shenzhen)Co.,ltd. All rights reserved.
//

#import "ViewController.h"
#import "PlistFileHelper.h"
#import "lvfjHttpRequest.h"
#import "MD5Util.h"
#import "StringUtils.h"
#import "lvfjCommon.h"
#import "NSDictionary+lvfj.h"

#define teltphone   @"18038939438"
#define kpwd         @"q14725836"

@interface ViewController ()

@property(copy) NSString *token;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [lvfjHttpRequest GET:@"http://test.blueup.cn/api/region/tree" parameters:@{} success:^(NSDictionary *dic) {
        NSMutableArray *allArr = [NSMutableArray array];
        
        NSDictionary *allDic = dic[@"data"];
        NSArray *allKeys = [allDic sortToKeyUp:YES];
        [allKeys enumerateObjectsUsingBlock:^(NSDictionary *obj1, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary  *obj = obj1[@"value"];
            NSMutableArray *cityList = [NSMutableArray array];
            [obj[@"i"] enumerateKeysAndObjectsUsingBlock:^(NSString* citykey, NSDictionary* cityobj, BOOL * _Nonnull stop) {
                NSMutableArray *areaList = [NSMutableArray array];
                if ([cityobj[@"i"] count]) {
                    [cityobj[@"i"] enumerateKeysAndObjectsUsingBlock:^(NSString* areakey, NSDictionary* areaobj, BOOL * _Nonnull stop) {
                        [areaList addObject:@{@"code":areakey,@"name":areaobj}];
                    }];
                }else{
                    [areaList addObject:@{@"code":@"",@"name":
                    @""}];
                }

                [cityList addObject:@{@"code":citykey,@"name":cityobj[@"n"],@"areaList":areaList}];
            }];
            [allArr addObject:@{@"code":obj1[@"key"],@"name":obj[@"n"],@"cityList":cityList}];
        }];
        
        NSLog(@"%@",allArr);
//        [PlistFileHelper saveCatchHtml:@""];
        [PlistFileHelper saveDicToJson:allArr key:nil docName:@"offfline.json"];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark sainiuhui

-(void)sainiuhui{
    __weak typeof(self) wSelf = self;
    [lvfjHttpRequest POST:@"http://api.sainiuhui.com/user/login"  parameters:@{@"pwd":kpwd,@"phone":teltphone} success:^(NSDictionary *dic) {
        lvfjLog(@"%@",dic);
        __strong typeof(wSelf) sSelf = wSelf;
        sSelf.token = dic[@"data"][@"token"];
    } failure:^(NSError *error) {
        lvfjLog(@"%@",error);
    }];
    
    [PlistFileHelper saveDicToPlist:@{@"qwer":@"qewr"} key:@"qwr"];
    
    BOOL result = [StringUtils isValidateIDCard:@"440221199002146210"];
    lvfjLog(@"%d",result);
    result = [StringUtils isValidateEmail:@"280482431@qq.com"];
    NSLog(@"%d",result);
    result = [StringUtils isValidateEmail:@"lvfeijun@guodu.com.hk"];
    lvfjLog(@"%d",result);
}

-(void)sed{
    NSString *sig = [MD5Util encryptByMD5String:[NSString stringWithFormat:@"sdkfj2f98ffh920f2fh%@",teltphone]];
    [lvfjHttpRequest POST:@"http://api.sainiuhui.com/kapi/phonecode/phone"  parameters:@{@"Token":self.token,@"phone":teltphone,@"sig":sig} success:^(NSDictionary *dic) {
        NSLog(@"%@",dic);
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
